using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_MallNormalPanel : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_MallNormalPanel
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject leftToggle { protected set; get; }
            public UnityEngine.UI.ExLoopHorizontalScrollRect mallItemLoopListShort { protected set; get; }
            public UnityEngine.UI.ExLoopHorizontalScrollRect mallItemLoopListLong { protected set; get; }
            public UnityEngine.RectTransform itemArea { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("leftToggle", out var __tbv0);
                this.leftToggle = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopHorizontalScrollRect>("mallItemLoopListShort", out var __tbv1);
                this.mallItemLoopListShort = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopHorizontalScrollRect>("mallItemLoopListLong", out var __tbv2);
                this.mallItemLoopListLong = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.RectTransform>("itemArea", out var __tbv3);
                this.itemArea = __tbv3;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private LoopScrollAdapter<UIN_MallItemCellShort> shortItems = null;
        private LoopScrollAdapter<UIN_MallItemCellLong> longItems = null;
        private CfgMallType cfgMallType = null;
        private List<int> mallItemCidList = null;
        #endregion fields

        #region properties

        protected UIB_UIN_MallNormalPanel ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_MallNormalPanel();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.mallItemCidList = ListPool<int>.Get();

            this.shortItems = this.InitLoopScroll<UIN_MallItemCellShort>(this.ui.mallItemLoopListShort, this.OnShortItemChanged);
            this.shortItems.InitUnitSelected(UnitSelectType.ByExButton, this.OnShortItemSelected);
            this.longItems = this.InitLoopScroll<UIN_MallItemCellLong>(this.ui.mallItemLoopListLong, this.OnLongItemChanged);
            this.longItems.InitUnitSelected(UnitSelectType.ByExButton, this.OnLongItemSelected);

            this.shortItems.InitUnitUpdate();
            this.longItems.InitUnitUpdate();
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.shortItems = null;
            this.longItems = null;
            this.cfgMallType = null;
            ListPool<int>.Put(this.mallItemCidList);
        }

        #region methonds

        public void Show(CfgMallType cfgMallType, bool isShowSubType)
        {
            this.cfgMallType = cfgMallType;
            this.InitDataList(cfgMallType.Id);

            this.Show();
            this.ShowLeftReturnIsChanged(isShowSubType);
            this.ShowListView();
        }

        private bool ShowLeftReturnIsChanged(bool isActive)
        {
            bool isChanged = this.ui.leftToggle.activeSelf != isActive;
            if (isChanged)
            {
                this.ui.leftToggle.SetActive(isActive);
            }
            return isChanged;
        }

        private void InitDataList(int mallTypeCid)
        {
            var listCids = TableCenter.mallItem.MallItemMap[mallTypeCid];
            this.mallItemCidList.Clear();
            for (int i = 0; i < listCids.Count; i++)
            {
                int mallItemCid = listCids[i];
                if (MallModule.Instance.CheckMallItemIsCanShow(mallItemCid))
                {
                    this.mallItemCidList.Add(mallItemCid);
                }
            }
        }

        private void ShowListView()
        {
            switch (cfgMallType.ShowType)
            {
                case MallShowType.ShortCell:
                    this.longItems.SetActive(false);
                    this.shortItems.RefillCells(this.mallItemCidList.Count);
                    break;
                case MallShowType.LongCell:
                    this.shortItems.SetActive(false);
                    this.longItems.RefillCells(this.mallItemCidList.Count);
                    break;
                default:
                    break;
            }
        }

        private void OnShortItemChanged(UIN_MallItemCellShort item, int index)
        {
            item.Show(this.mallItemCidList[index]);
        }

        private void OnShortItemSelected(int index, UIN_MallItemCellShort item)
        {
            this.OpenUI<UIBuyPanel>(UD_BuyPanel.Create(item.mallItemId));
        }

        private void OnLongItemChanged(UIN_MallItemCellLong item, int index)
        {
            item.Show(this.mallItemCidList[index]);
        }

        private void OnLongItemSelected(int index, UIN_MallItemCellLong item)
        {
            this.OpenUI<UIBuyPanel>(UD_BuyPanel.Create(item.mallItemId));
        }
        #endregion methonds
    }
}
